package com.yeban.javase.day14.test1;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/24
 *
 * 读入文本
 * 用正则匹配每一段（提取“编号”和“内容”）
 * 存入 Map（编号→内容）
 * 把编号转为数字以便排序
 * 按顺序输出
 */
public class testSort {

    public static void main(String[] args) {
        File file = new File("src/com/yeban/javase/day14/test1/demo.txt");
        try(
                BufferedReader br = new BufferedReader(new FileReader(file));
                PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("src/com/yeban/javase/day14/test1/demo2.txt")));
        ) {
            String line = null;
            List<String> lines = new ArrayList<>();
            while((line = br.readLine()) != null) {
                lines.add(line);
            }

            // 按照
            List<String> collect = lines.stream().sorted((e1, e2) -> {
                // 提取每段的编号
                String a = e1.substring(0, 1);
                String b = e2.substring(0, 1);
                return exChange(a) - exChange(b);
            }).collect(Collectors.toList());

            for(String s : collect) {
                pw.println(s);
            }
            System.out.println("写入完成！");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static int exChange(String str) {
        switch (str) {
            case "一":
                return 1;
            case "二":
                return 2;
            case "三":
                return 3;
            case "四":
                return 4;
            case "五":
                return 5;
            case "六":
                return 6;
            case "七":
                return 7;
            case "八":
                return 8;
            case "九":
                return 9;
        }
        return -1;
    }
}
